package cn.edu.zust.lxy.wangke_android;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.edu.zust.lxy.wangke_android.util.HttpUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;


public class MainActivity extends AppCompatActivity {

    private Button btn_login;
    private EditText username;
    private EditText password;
    private Button btn_register;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_login = findViewById(R.id.login);
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
        btn_register = findViewById(R.id.to_register);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
        
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = username.getText().toString();
                String pass = password.getText().toString();
                login(name, pass);
            }
        });
    }

    private void login(String name, String pass) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(HttpUtil.baseurl +
                                    "/user/login?" +
                                    "user_name=" + name +
                                    "&user_password=" + pass)
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String result = response.body().string();
                    JSONObject json = new JSONObject(result);
                    int code = json.getInt("code");
                    if (code == 200) {
                        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("username", name);
                        editor.commit();
                        Intent intent = new Intent(MainActivity.this, LibraryActivity.class);
                        startActivity(intent);
                        finish();
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "登录代码有问题啊！！", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();

    }
}